﻿using Cinch;
using FR.SAFTAnalyserLite.Model;
using FR.SAFTAnalyserLite.ViewModels.Pane;
using MEFedMVVM.Common;
using System.Linq;

namespace FR.SAFTAnalyserLite.ViewModels
{
    public class SupplierViewModel : DocumentPartViewModel
    {
        public SupplierViewModel()
            : base("Fornecedores")
        {
            if (!Designer.IsInDesignMode)
            {
                Mediator.Instance.Register(this);
            }
        }

        internal override void ResetSaftFile()
        {
            this.Suppliers = null;
        }

        [MediatorMessageSink(MessageType.SAFT_FILE_OPENED)]
        private void LoadSuppliers(AuditFile saft)
        {
            if (saft != null && saft.MasterFiles != null)
                this.Suppliers = saft.MasterFiles.Supplier;
        }

        Supplier[] suppliers;
        public Supplier[] Suppliers
        {
            get { return suppliers; }
            set
            {
                suppliers = value;
                NotifyPropertyChanged(ObservableHelper.CreateArgs<SupplierViewModel>(x => x.Suppliers));
            }
        }

        string filter;
        public string Filter
        {
            get { return filter; }
            set
            {
                filter = value;

                if (string.IsNullOrEmpty(filter))
                    this.Suppliers = SAFT.SaftAnalyser.Instance.SaftFile.MasterFiles.Supplier;
                else
                {
                    this.Suppliers = (from c in SAFT.SaftAnalyser.Instance.SaftFile.MasterFiles.Supplier
                                      where !string.IsNullOrEmpty(c.AccountID) && c.AccountID.IndexOf(filter, System.StringComparison.OrdinalIgnoreCase) >= 0 ||
                                          !string.IsNullOrEmpty(c.CompanyName) && c.CompanyName.IndexOf(filter, System.StringComparison.OrdinalIgnoreCase) >= 0 ||
                                          !string.IsNullOrEmpty(c.Contact) && c.Contact.IndexOf(filter, System.StringComparison.OrdinalIgnoreCase) >= 0 ||
                                          !string.IsNullOrEmpty(c.Email) && c.Email.IndexOf(filter, System.StringComparison.OrdinalIgnoreCase) >= 0 ||
                                          !string.IsNullOrEmpty(c.Fax) && c.Fax.IndexOf(filter, System.StringComparison.OrdinalIgnoreCase) >= 0 ||
                                          !string.IsNullOrEmpty(c.SupplierID) && c.SupplierID.IndexOf(filter, System.StringComparison.OrdinalIgnoreCase) >= 0 ||
                                          !string.IsNullOrEmpty(c.SupplierTaxID) && c.SupplierTaxID.IndexOf(filter, System.StringComparison.OrdinalIgnoreCase) >= 0 ||
                                          !string.IsNullOrEmpty(c.Telephone) && c.Telephone.IndexOf(filter, System.StringComparison.OrdinalIgnoreCase) >= 0 ||
                                          !string.IsNullOrEmpty(c.Website) && c.Website.IndexOf(filter, System.StringComparison.OrdinalIgnoreCase) >= 0
                                      select c).ToArray();
                }
            }
        }
    }
}
